import java.util.*;

class Solution2 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> hash = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] arr = strs[i].toCharArray();
            Arrays.sort(arr);
            String s = String.valueOf(arr);
            if (!hash.containsKey(s)) {
                hash.put(s, new ArrayList());
            }
            hash.get(s).add(strs[i]);
        }
        return new ArrayList(hash.values());
    }
}